<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace ChannelThrift\Service;

use Base\BaseService;

/**
* 汽车车型功能类
* @author       Eric
*/
class CarModelService extends BaseService {

    public $cm_db_prefix=null;
    public $cm_dsn=null;

    public $car_mbrand = null;
    public $car_brand  = null;
    public $car_series = null;
    public $car_model  = null;
    /**
     * @param string $db_prefix 表前缀
     * @param string $dsn   数据源名称
     */
    public function __construct($db_prefix='',$dsn='') {
        $this->cm_db_prefix = $db_prefix;
        $this->cm_dsn = $dsn;

        $this->car_mbrand = D('ChannelThrift/Aums/CarMbrand');
        $this->car_brand = D('ChannelThrift/Aums/CarBrand');
        $this->car_series = D('ChannelThrift/Aums/CarSeries');
        $this->car_model = D('ChannelThrift/Aums/CarModel');
    }

    /**
    * 获得车型的父品牌信息
    * @param int $model_id 车型id
    * @return array 父品牌信息
    */
    public function get_parent_brand_info($model_id){
        if(!is_numeric($model_id))
            return false;
        $info = array();
        $model =$this->car_model->where('model_id='.$model_id)->find();
        if($model){
            $info['model'] = $model;
            $series =$this->car_series->where('series_id='.$model['series_id'])->find();
        }
        if($series){
            $info['series'] = $series;
            $brand =$this->car_brand->where('brand_id='.$series['brand_id'])->find();
        }
        if($brand){
            $info['brand'] = $brand;
            $mbrand =$this->car_mbrand->where('mbrand_id = '.$brand['mbrand_id'])->find();
        }
        if($mbrand){
            $info['mbrand'] = $mbrand;
        }
        return $info;
    }

    /**
    * 获得主品牌的子品牌信息
    * @param int $mbrand_id 主品牌id  null获得所有品牌
    * @return array 子车型信息
    */
    public function get_brand_by_mbrand($mbrand_id=false){
        if($mbrand_id)
            $car_brands =$this->car_brand->where('status=0 AND mbrand_id='.$mbrand_id)->select();
        else
            $car_brands =$this->car_brand->where("status=0")->select();
        return $car_brands;
    }

    //获得所有主品牌
    public function get_mbrands(){
            $car_mbrands =$this->car_mbrand->where('status=0')->select();
            return $car_mbrands;
    }

   //获得主品牌的所有车系
    public function get_series_by_mbrand($mbrand_id){
        $series = $this->car_series->query("SELECT b.name bname,b.brand_id ,s.*FROM au_car_series s
                                LEFT JOIN au_car_brand b ON s.brand_id = b.brand_id 
                                LEFT JOIN au_car_mbrand m ON b.mbrand_id = m.mbrand_id
                                WHERE s.status=0 AND m.mbrand_id='".$mbrand_id."'");
        return $series;
    }

    /**
    * 获得品牌的子车系信息
    * @param int $brand_id 品牌id
    * @return array 子车型信息
    */
    public function get_series_by_brand($brand_id){
        $series =$this->car_series->where('status=0 AND brand_id='.$brand_id)->select();
        return $series;
    }

    /**
    * 获得车系的车型信息
    * @param int $series_id 车系id
    * @return array 车型信息
    */
    public function get_model_by_series($series_id){
        $models =$this->car_model->where('status=0 AND series_id='.$series_id)->select();
        return $models;
    }

    /**
    * 获得品牌、车系、车型信息
    * @param int $id id
    * @param string $type 类型(mbrand,brand,series,model)
    * @return array
    */
    public function get_model_by_id($id,$type='model'){
            return $this->{'car_'.$type}->where($type."_id='".$id."'")->find();
    }
    
    /**
     * 通过car_id获取汽车手续信息 
     * @param unknown $id
     * @return \Think\mixed
     */
    public function get_procedure_by_car_id($id)
    {
        $map = array();
        $map['car_id'] = $id;
        return M('car_procedure')->where($map)->find();
    }

    //---------------------------车型库管理--------------------------------------

    /**
     * 车型库管理
     * 添加品牌，车系，车型
     * @param $data 新的数据
     * @param string $table
     * @return mixed|string
     */
    public function add($data,$table='car_mbrand'){
        $data['update_time'] = date('Y-m-d H:i:s');
        $rs = M($table,$this->cm_db_prefix,$this->cm_dsn)->add($data);
        return $rs;
    }

    /**
     * 车型库管理
     * 更新品牌，车系，车型
     * @param $data 新的数据
     * @param array $map
     * @param string $table
     * @author Eric
     * @date 2016-05-12
     * @return bool
     */
    public function update($data,$map=array(),$table='car_mbrand'){
        $data['update_time'] = date('Y-m-d H:i:s');
        $rs = M($table,$this->cm_db_prefix,$this->cm_dsn)->where($map)->save($data);
        return $rs;
    }

    /**
     * 车型库管理
     * 获取主品牌、品牌、车系、车型列表
     * @param int $page
     * @param int $size
     * @param array $map
     * @param string $table
     * @author Eric
     * @date 2016-05-12
     * @return array|mixed
     */
    public function get_data($page=0,$size=20,$map=array(),$table='mbrand'){
            $total = M('car_'.$table,$this->cm_db_prefix,$this->cm_dsn)->where($map)->count();
            $data = M('car_'.$table,$this->cm_db_prefix,$this->cm_dsn)->where($map)->limit($size)->page($page)->select();
            return array($data,$total);
    }

    /**
     * 车型库管理
     * 数据库同步添加
     * @param $data
     * @param string $table
     * @param $db_prefix
     * @param $dsn
     * @author Eric
     * @date 2016-05-12
     * @return mixed
     */
    public function sync_add($data,$table='car_mbrand',$db_prefix,$dsn){
        $data['update_time'] = date('Y-m-d H:i:s');
        $rs = M($table,$db_prefix,$dsn)->add($data);
        return $rs;

    }

    /**
     * 车型库管理
     * 数据库同步更新
     * @param $data
     * @param array $map
     * @param string $table
     * @param $db_prefix
     * @param $dsn
     * @author Eric
     * @date 2016-05-12
     * @return bool
     */
    public function sync_update($data,$map=array(),$table='car_mbrand',$db_prefix,$dsn){
            $data['update_time'] = date('Y-m-d H:i:s');
            $rs = M($table,$db_prefix,$dsn)->where($map)->save($data);
            return $rs;
    }


    /**
     * 车型库管理
     * 获取首字母
     * @author Eric
     * @date 2016-05-12
     * @return array
     */
    public function getFirstChar() {
        $chars = M('car_mbrand',$this->cm_db_prefix,$this->cm_dsn)->field('DISTINCT `first_char`')->order('first_char ASC')->select();
        $first = array();
        foreach($chars AS $val) {
            $first[] = $val['first_char'];
        }
        return $first;
    }

    /**
     * 车型库管理
     * 获取品牌厂商
     * @author Eric
     * @date 2016-05-12
     * @return mixed
     */
    public function get_manufacturer(){
        $map = array();
        $map['status']  = array('eq',0);
        $ret = M('car_manufacturer',$this->cm_db_prefix,$this->cm_dsn)->where($map)->order('spell ASC')->select();
        return $ret;
    }

    /**
     * 车型库管理
     * 获取厂商信息
     * @param $manufacturer_id
     * @author Eric
     * @date 2016-05-12
     * @return mixed
     */
    public function get_manufacturer_info($manufacturer_id){
        $map = array();
        $map['manufacturer_id']  = array('eq',$manufacturer_id);
        $ret = M('car_manufacturer',$this->cm_db_prefix,$this->cm_dsn)->where($map)->find();
        return $ret;
    }

    /**
     * 车型库管理
     * 车源展示项
     * @author Eric
     * @date 2016-05-12
     * @return array
     */
    public function getModelCols() {
            return array(
                '车型' => C('CAR_TYPE'),
                '厂商指导价' => 'text',
                '全国4S店最低价' => 'text',
                '排量(L)' => 'text',
                '长*宽*高(mm)' => 'text',
                //'变速箱类型' => 'text',
                '助力类型' => C('CAR_MODEL_HELPER_TYPE'),
                '前制动' => C('CAR_MODEL_FRONT_BRAKE_TYPE'),
                '后制动' => C('CAR_MODEL_BACK_BRAKE_TYPE'),
                '欧IV+OBD' => 'text',
                '排放标准' => C('EMISSION'),
                '燃油' => C('FUEL_TYPE'),
                '座位数' => 'text',
                '倒车影像系统' => '',
                '车内中控锁' => '',
                '无钥匙启动系统' => '',
                '遥控钥匙' => '',
                '刹车辅助(EBA/BAS/BA等)' => '',
                '车身稳定控制(ESP/DSC/VSC等)' => '',
                '电子制动力分配系统' => '',
                '防抱死制动系统' => '',
                '定速巡航' => '',
                '前驻车雷达' => '',
                '自动泊车入位' => '',
                '方向盘换挡' => '',
                '全景天窗' => '',
                '电动天窗' => '',
                '前电动车窗' => '',
                '后电动车窗' => '',
                '车窗防夹手功能' => '',
                '真皮座椅' => '',
                '前排座椅加热' => '',
                '后排座椅加热' => '',
                '电动座椅记忆' => '',
                '自动折叠后视镜' => '',
                '后视镜加热' => '',
            );
    }

    /**
     * 车型库管理
     * 排量转换
     * @author Eric
     * @date 2016-05-12
     * @return array
     */
    public function configParse($config) {
            if($config['排量(mL)']) {
                $config['排量(L)'] = sprintf("%.2f",$config['排量(mL)']/1000);
            }
            return $config;
    }

    /**
     * 车型库管理
     * 车源字段展示
     * @author Eric
     * @date 2016-05-12
     * @return array
     */
    public function tableCols() {
            return array(
                'produce_status' => array('name' => '生产状态','value' => array('在产','停产')),
                'sale_state' => array('name' => '销售状态','value' => array('在销','停销')),
                'add_press_type' => array('name' => '增压类型','value' => array('无','涡轮增压','双涡轮增压','机械增压','涡轮机械双增压')),
                'gear_box_type' => array('name' => '变速箱类型','value' => array('手动','自动','手自一体','CVT无级变速', '双离合', '半自动', '电动车单速变速箱')),
                'car_year_type' => array('name' => '出厂年份','value' => false),
                'car_refer_price' => array('name' => '厂商指导价(万元)','value' => false),
                'emission' => array('name' => '档位个数','value' => false),
                'exhaust' => array('name' => '排量(L)','value' => false),
                'consumption' => array('name' => '油耗','value' => false)
            );
    }
}

?>